var express = require('express');
var router = express.Router();
// var query = require("../db/db").query
var queryPromise = require("../db/db").queryPromise
/* GET home page. */
router.get('/', async function (req, res, next) {
  // console.log(req)
  console.log(req.query)  //从前台传来的参数
  //1.获取total总数
  let sql_cityCount = "select count(*) from city"
  let count_res = await queryPromise(sql_cityCount,[])
  let total = count_res.results[0]["count(*)"]
  // console.log(total)
  //2.根据req.query数据中的 { start: '0', pageSize: '20' } 获取分页数据
  let sql_cityList = "select * from city limit ?,?"
  let values = [req.query.start-0,req.query.pageSize-0]
  let cityList_res = await queryPromise(sql_cityList,values)
  let data = []
  cityList_res.results.map(v=>{
    data.push({
      ID:v.ID,
      Name:v.Name,
      Population:v.Population
    })
  })
  res.json({
    code:1,
    total:total,
    data:data
  })
});

module.exports = router;